import panel from './panel.vue'
const HAZARDS_LEVELS = [
  "evaluation_level_one",
  "evaluation_level_two",
  "evaluation_level_three",
  "evaluation_level_four"
];
const source = {
  collectionName: "security_risk_filing",
  options: { include: ["gridId"], exists: "Coordinate" }
}

const layer = {
  source: ["hazards", "grid"],
  mergeSource(obj) {
    let { grid, hazards } = obj;
    hazards = hazards.map(item => {
      let obj = grid.find(v => v.id === item.gridId);
      item.gridPath = obj ? [].concat(obj.paths, obj.id) : [];
      return item;
    });
    grid = grid.map(item => {
      let children = hazards.filter(
        v => v.gridPath.indexOf(item.id) > -1
      );
      if (children.length) {
        item.hazards = {
          source: children,
          level: _.groupBy(children, "level")
        };
      }
      return { ...item };
    });
    return { grid, hazards };
  },
  zoom: [16, 30],
  infoWindow: {
    component: panel,
    title: "name"
  },
  marker: {
    coordinate: "Coordinate",
    src: [
      require("./危险源_lv1.png"),
      require("./危险源_lv2.png"),
      require("./危险源_lv3.png"),
      require("./危险源_lv4.png")
    ],
    rule: prop => HAZARDS_LEVELS.indexOf(prop.level)
  }
}

export default {
  sources: { hazards: source },
  layers: { hazards: layer }
}
